package com.morgan.maxfunding;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

public class UsbActivity2 extends Activity {
    
	LinearLayout llytHeader;

    Button btnInfAnyBusiness;
    Button btnInfPersonWithGood;
    
    Button btnNoPersonWithGood;
    Button btnYesPersonWithGood;
    
    EditText	edtItemList1;
    EditText	edtItemList2;
    EditText	edtItemList3;
    EditText	edtItemList4;
    
    EditText 	edtCurrentValue1;
    EditText 	edtCurrentValue2;
    EditText 	edtCurrentValue3;
    EditText 	edtCurrentValue4;

    EditText	edtOtherIncome;
    Button		btnSubmit;
    
    String 		strGuaranator = "";
    
    @Override
	protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_usb2);
        
        initWidget();
        initValue();
        initEvent();
    }
    
    private void initWidget() {
    	
		llytHeader			= (LinearLayout) findViewById(R.id.header_linearLayout);

    	btnInfAnyBusiness 		= (Button) findViewById(R.id.inf_any_business_equipment_button);
    	btnInfPersonWithGood 	= (Button) findViewById(R.id.inf_person_with_good_history_button);
    	
    	btnNoPersonWithGood		= (Button) findViewById(R.id.no_person_with_good_button);
    	btnYesPersonWithGood	= (Button) findViewById(R.id.yes_person_with_good_button);
    	
    	edtItemList1 = (EditText) findViewById(R.id.itemlist_editText1);
    	edtItemList2 = (EditText) findViewById(R.id.itemlist_editText2);
    	edtItemList3 = (EditText) findViewById(R.id.itemlist_editText3);
    	edtItemList4 = (EditText) findViewById(R.id.itemlist_editText4);
    	
    	edtCurrentValue1 = (EditText) findViewById(R.id.usb2_current_value_editText1);
    	edtCurrentValue2 = (EditText) findViewById(R.id.usb2_current_value_editText2);
    	edtCurrentValue3 = (EditText) findViewById(R.id.usb2_current_value_editText3);
    	edtCurrentValue4 = (EditText) findViewById(R.id.usb2_current_value_editText4);
    	
    	edtOtherIncome = (EditText) findViewById(R.id.other_income_editText);
    	
    	btnSubmit	= (Button) findViewById(R.id.usb2_submit_button);
    }
    
    private void initValue() {

    }
    
    private void initEvent() {
    	
		llytHeader.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://maxfunding.com.au"));
				startActivity(browserIntent);
			}
        });
		
    	btnInfAnyBusiness.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				showGeneralAlert("Information", "Please list down business assets that you fully own. E.g. computers, printer, kitchenware, business machinery. Note: Don't list down assets that are financed.");
			}
        });
    	
    	btnInfPersonWithGood.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				showGeneralAlert("Information", "Having a guarantor on this loan can strengthen your application. (Whether you already have good credit or not, it would still help)");
			}
        });
    	
    	btnNoPersonWithGood.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				strGuaranator = "No";
				btnNoPersonWithGood.setBackgroundResource(R.drawable.radio_yes);
				btnYesPersonWithGood.setBackgroundResource(R.drawable.radio_no);
			}
	    });
    	btnYesPersonWithGood.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				strGuaranator = "Yes";
				btnNoPersonWithGood.setBackgroundResource(R.drawable.radio_no);
				btnYesPersonWithGood.setBackgroundResource(R.drawable.radio_yes);
			}
	    });
    	
    	btnSubmit.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (!checkInput()) return;
				
				new MyAsyncTask(UsbActivity2.this, MyAsyncTask.ACTION_USB).execute("usb");
			}
        });
    }
    
	public void showGeneralAlert(String title, String message) {
    	new AlertDialog.Builder(UsbActivity2.this)
        .setTitle(title)
        .setMessage(message)
        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                // continue with delete
            }
         })
         .show();
    }
	
	public boolean checkInput() {
		
		String str;
		
		if (strGuaranator.equals("")) {
			showGeneralAlert("Question", "Can a person with a good credit history be your guarantor?");
			return false;
		}
		str = edtOtherIncome.getText().toString();
		if (str.equals("")) {
			showGeneralAlert("Input", "Will your be receiving any income beside potential profits for this business?\nIf yes, please state amount and income frequencies.");
			return false;
		}
		
		GlobalData.personalInfo.strItemList1 = edtItemList1.getText().toString();
		GlobalData.personalInfo.strItemList2 = edtItemList2.getText().toString();
		GlobalData.personalInfo.strItemList3 = edtItemList3.getText().toString();
		GlobalData.personalInfo.strItemList4 = edtItemList4.getText().toString();
		
		GlobalData.personalInfo.strCurrentValue1 = edtCurrentValue1.getText().toString();
		GlobalData.personalInfo.strCurrentValue2 = edtCurrentValue2.getText().toString();
		GlobalData.personalInfo.strCurrentValue3 = edtCurrentValue3.getText().toString();
		GlobalData.personalInfo.strCurrentValue4 = edtCurrentValue4.getText().toString();
		
		GlobalData.personalInfo.strGuaranator = strGuaranator;
		GlobalData.personalInfo.strOtherIncomeUSB = edtOtherIncome.getText().toString();
		GlobalData.personalInfo.nTypeUSB = 2;

		return true;
	}
	
	public void goNextPage() {
		startActivity(new Intent(this, DeclineActivity.class));
	}
}
